<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\mobilenode\controller;

class IndexAdminOrder extends \app\ApiRest
{
    protected $model;
    protected $refund_order_model;
    protected $comment_model;
    protected $admin_info;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Order();
        $this->refund_order_model = new \app\massage\model\RefundOrder();
        $this->comment_model = new \app\massage\model\Comment();
        $this->admin_info = $this->adminInfo();
        if(empty($this->admin_info)) {
            $this->errorMsg("你还没有管理员权限");
        }
    }
    public function adminInfo()
    {
        $admin_model = new \app\mobilenode\model\RoleAdmin();
        $dis = ["user_id" => $this->getUserId(), "status" => 1];
        $data = $admin_model->dataInfo($dis);
        if(empty($data)) {
            $this->errorMsg("你还没有管理员权限");
        }
        if(!empty($data["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $admin = $admin_model->dataInfo(["id" => $data["admin_id"]]);
            $data["admin_arr"] = $admin_model->getAdminId($admin);
            $data["phone_encryption"] = $admin["phone_encryption"];
            $data["agent_coach_auth"] = $admin["agent_coach_auth"];
        } else {
            $data["phone_encryption"] = 1;
            $data["agent_coach_auth"] = 1;
        }
        return $data;
    }
    public function index()
    {
        $order_type = [2, 3, 4, 5, 6, 7];
        $dis = ["uniacid" => $this->_uniacid, "is_add" => 0];
        $where = [];
        if(!empty($this->admin_info["admin_id"])) {
            $where[] = ["admin_id", "in", $this->admin_info["admin_arr"]];
        }
        foreach ($order_type as $value) {
            $data["order_count"][$value] = $this->model->where($dis)->where($where)->where(["pay_type" => $value])->count();
        }
        $data["refuse_order"][8] = $this->model->where($dis)->where($where)->where(["pay_type" => 8])->count();
        $dis["is_add"] = 1;
        $order_type = [2, 3, 6, 7];
        foreach ($order_type as $value) {
            if($value == 6) {
                $data["add_count"][$value] = $this->model->where($dis)->where($where)->where("pay_type", "in", [4, 5, 6])->count();
            } else {
                $data["add_count"][$value] = $this->model->where($dis)->where($where)->where(["pay_type" => $value])->count();
            }
        }
        $order_type = [1];
        foreach ($order_type as $value) {
            $data["add_refund_count"][$value] = $this->refund_order_model->where($dis)->where($where)->where(["status" => $value])->count();
        }
        $dis["is_add"] = 0;
        foreach ($order_type as $value) {
            $data["refund_count"][$value] = $this->refund_order_model->where($dis)->where($where)->where(["status" => $value])->count();
        }
        $data["node"] = $this->admin_info["node"];
        $data["agent_coach_auth"] = $this->admin_info["agent_coach_auth"];
        $data["is_agent"] = !empty($this->admin_info["admin_id"]) ? 1 : 0;
        $notice_model = new \app\massage\model\NoticeList();
        $admin_arr = !empty($this->admin_info["admin_id"]) ? $this->admin_info["admin_arr"] : 0;
        $data = $notice_model->indexOrderNotice($data, $this->_uniacid, $admin_arr, $this->admin_info["node"]);
        return $this->success($data);
    }
    public function orderList()
    {
        $input = $this->_param;
        $is_add = isset($input["is_add"]) ? $input["is_add"] : 0;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["is_add", "=", $is_add];
        if(!empty($this->admin_info["admin_id"])) {
            $dis[] = ["admin_id", "in", $this->admin_info["admin_arr"]];
        }
        if(!empty($input["pay_type"])) {
            $dis[] = ["pay_type", "=", $input["pay_type"]];
        } else {
            $dis[] = ["pay_time", ">", 0];
            $dis[] = ["pay_type", "not in", [8]];
        }
        if(!empty($input["order_code"])) {
            $dis[] = ["order_code", "like", "%" . $input["order_code"] . "%"];
        }
        $data = $this->model->where($dis)->field("id,material_price,coach_id,store_id,true_car_price,is_comment,order_code,true_service_price,pay_type,pay_price,start_time,create_time,user_id,end_time,add_pid,is_add")->order("id desc")->paginate(10)->toArray();
        if(!empty($data["data"])) {
            $order_data_model = new \app\massage\model\OrderData();
            $order_goods_model = new \app\massage\model\OrderGoods();
            $abn_model = new \app\abnormalorder\model\OrderList();
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["start_time"] = date("Y-m-d H:i", $v["start_time"]);
                $v["end_time"] = date("Y-m-d H:i", $v["end_time"]);
                $v["can_refund_price"] = $this->model->getOrderRefundPrice($v);
                $v["add_flow_path"] = $order_data_model->where(["order_id" => $v["id"]])->value("add_flow_path");
                if($v["is_add"] == 0) {
                    $v["add_service"] = $order_goods_model->getAddOrderGoods($v["id"]);
                }
                if($v["is_add"] == 0) {
                    $v["admin_apply_refund"] = $this->model->orderCanRefund($v["id"]);
                }
                $v["abn_order_id"] = $abn_model->where(["order_id" => $v["id"]])->value("id");
            }
        }
        return $this->success($data);
    }
    public function orderInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->model->dataInfo($dis);
        $data["pay_order_times"] = $this->model->useOrderTimes($data["user_id"], $data["create_time"]);
        $data["time_long"] = $data["true_time_long"];
        $data["distance"] = distance_text($data["distance"]);
        if($data["is_add"] == 0) {
            $data["add_order_id"] = $this->model->where(["add_pid" => $data["id"]])->where("pay_type", ">", 1)->field("id,order_code")->select()->toArray();
        } else {
            $data["add_pid"] = $this->model->where(["id" => $data["add_pid"]])->field("id,order_code")->find();
        }
        $order_model = new \app\massage\model\OrderData();
        $order_data = $order_model->dataInfo(["order_id" => $input["id"], "uniacid" => $this->_uniacid]);
        $data = array_merge($order_data, $data);
        $change_log_model = new \app\massage\model\CoachChangeLog();
        $data["dispatch_record"] = $change_log_model->orderChangeLog($input["id"]);
        $admin_model = new \app\massage\model\Admin();
        $data["admin_name"] = $admin_model->where(["id" => $data["admin_id"]])->value("agent_name");
        if(!empty($data["store_id"])) {
            $store_model = new \app\massage\model\StoreList();
            $data["store_info"] = $store_model->where(["id" => $data["store_id"]])->field("title,cover,address,lng,lat,phone")->find();
        }
        $arr = ["create_time", "pay_time", "serout_time", "arrive_time", "receiving_time", "start_service_time", "order_end_time", "coach_refund_time", "sign_time"];
        foreach ($arr as $value) {
            $data[$value] = !empty($data[$value]) && 1 < $data[$value] ? date("Y-m-d H:i:s", $data[$value]) : 0;
        }
        $data["start_time"] = date("Y-m-d H:i", $data["start_time"]) . "-" . date("H:i", $data["end_time"]);
        $data["can_refund_price"] = $this->model->getOrderRefundPrice($data);
        $data["phone_encryption"] = $this->admin_info["phone_encryption"];
        $order_goods_model = new \app\massage\model\OrderGoods();
        if($data["is_add"] == 0) {
            $data["add_service"] = $order_goods_model->getAddOrderGoods($data["id"]);
        }
        if($data["is_add"] == 0) {
            $data["admin_apply_refund"] = $this->model->orderCanRefund($data["id"]);
        }
        if(!empty($this->admin_info["admin_id"])) {
            $data["order_auth"] = in_array($data["admin_id"], $this->admin_info["admin_arr"]) ? 1 : 0;
        } else {
            $data["order_auth"] = 1;
        }
        $abn_model = new \app\abnormalorder\model\OrderList();
        $data["abn_order_id"] = $abn_model->where(["order_id" => $data["id"]])->value("id");
        if(!empty($data["admin_id"]) && !empty($this->admin_info["admin_id"])) {
            $data["delegated_coach"] = $admin_model->where(["id" => $data["admin_id"]])->value("delegated_coach");
        } else {
            $data["delegated_coach"] = 1;
        }
        $log_model = new \app\massage\model\OrderLog();
        $data["map_type"] = $log_model->where(["order_id" => $data["id"], "map_type" => 1])->column("pay_type");
        $comm_model = new \app\massage\model\Commission();
        $find = $comm_model->where(["order_id" => $data["id"], "type" => 13])->count();
        $data["car_admin"] = 0 < $find ? 1 : 0;
        if($data["pay_model"] == 4) {
            $share_model = new \app\balancediscount\model\OrderShare();
            $balance_discount_data = $share_model->orderShareData($data["id"]);
            $data = array_merge($data, $balance_discount_data);
        }
        return $this->success($data);
    }
    public function refundOrderInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->refund_order_model->dataInfo($dis);
        $data["pay_order_code"] = $this->model->where(["id" => $data["order_id"]])->value("order_code");
        $data["create_time"] = date("Y-m-d H:i:s", $data["create_time"]);
        $data["refund_time"] = !empty($data["refund_time"]) ? date("Y-m-d H:i:s", $data["refund_time"]) : "";
        $pay_order = $this->model->dataInfo(["id" => $data["order_id"]]);
        $data["car_type"] = $pay_order["car_type"];
        $data["pay_type"] = $pay_order["pay_type"];
        $data["distance"] = distance_text($pay_order["distance"]);
        $data["pay_car_price"] = $pay_order["car_price"];
        $admin_model = new \app\massage\model\Admin();
        $data["admin_name"] = $admin_model->where(["id" => $data["admin_id"]])->value("agent_name");
        $data["store_id"] = $pay_order["store_id"];
        if(!empty($pay_order["store_id"])) {
            $store_model = new \app\massage\model\StoreList();
            $data["store_info"] = $store_model->where(["id" => $pay_order["store_id"]])->field("title,cover,address,lng,lat,phone")->find();
        }
        $data["phone_encryption"] = $this->admin_info["phone_encryption"];
        $data["check_user_name"] = $this->refund_order_model->checkUserName($data);
        return $this->success($data);
    }
    public function refundOrderList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.type", "=", 1];
        if(!empty($input["status"])) {
            $dis[] = ["a.status", "=", $input["status"]];
        } else {
            $dis[] = ["a.status", ">", -1];
        }
        if(!empty($this->admin_info["admin_id"])) {
            $dis[] = ["d.admin_id", "in", $this->admin_info["admin_arr"]];
        }
        if(!empty($input["order_code"])) {
            $dis[] = ["a.order_code", "like", "%" . $input["order_code"] . "%"];
        }
        $is_add = !empty($input["is_add"]) ? $input["is_add"] : 0;
        $dis[] = ["a.is_add", "=", $is_add];
        $data = $this->refund_order_model->indexAdminDataList($dis);
        return $this->success($data);
    }
    public function noPassRefund()
    {
        $input = $this->_input;
        $res = $this->refund_order_model->noPassRefund($input["id"], $this->admin_info["user_id"], 1, 3);
        if(!empty($res["code"])) {
            $this->errorMsg($res["msg"]);
        }
        return $this->success($res);
    }
    public function passRefund()
    {
        $input = $this->_input;
        $order = $this->refund_order_model->dataInfo(["id" => $input["id"]]);
        $is_app = $this->model->where(["id" => $order["order_id"]])->value("app_pay");
        $this->refund_order_model->dataUpdate(["id" => $input["id"]], ["version" => 1]);
        $res = $this->refund_order_model->passOrder($input["id"], $input["price"], $this->payConfig($this->_uniacid, $is_app), $this->admin_info["user_id"], $input["text"], 1, 3);
        if(!empty($res["code"])) {
            $this->errorMsg($res["msg"]);
        }
        return $this->success($res);
    }
    public function orderUpRecord()
    {
        $input = $this->_param;
        $order_model = new \app\massage\model\UpOrderList();
        $data = $order_model->orderUpRecord($input["order_id"]);
        return $this->success($data);
    }
    public function adminUpdateOrder()
    {
        $input = $this->_input;
        $order = $this->model->dataInfo(["id" => $input["order_id"]]);
        $update = $this->model->coachOrdertext($input, 1);
        $refund_model = new \app\massage\model\RefundOrder();
        $refund_order = $refund_model->where(["order_id" => $order["id"]])->where("status", "in", [1, 4, 5])->count();
        if(!empty($refund_order)) {
            $this->errorMsg("该订单正在申请退款，请先联系处理再进行下一步");
        }
        if(!empty($this->admin_info["admin_id"]) && !in_array($order["admin_id"], $this->admin_info["admin_arr"])) {
            $this->errorMsg("你没有权限");
        }
        $check = $this->model->checkOrderStatus($order["pay_type"], $input["type"], $order["is_add"]);
        if(!empty($check["code"])) {
            $this->errorMsg($check["msg"]);
        }
        $key = "adminUpdateOrder" . $input["order_id"] . "-" . date("Y-m-d H:i", time());
        incCache($key, 1, $this->_uniacid);
        if(getCache($key, $this->_uniacid) != 1) {
            decCache($key, 1, $this->_uniacid);
            $this->errorMsg("当前订单正在被操作，请稍后再试");
        }
        \think\facade\Db::startTrans();
        if($input["type"] == 7) {
            if($order["pay_type"] != 6 && !empty($order["coach_id"])) {
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::rollback();
                $this->errorMsg("订单状态错误，请刷新页面");
            }
            $res = $this->model->hxAddOrder($order, 0, 4, $this->getUserId());
            if(!empty($res["code"])) {
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::rollback();
                $this->errorMsg($res["msg"]);
            }
            $res = $this->model->hxOrder($order);
            if(!empty($res["code"])) {
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::rollback();
                $this->errorMsg($res["msg"]);
            }
        } elseif($input["type"] == -1) {
            $res = $this->model->cancelOrder($order);
            if(!empty($res["code"])) {
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::rollback();
                $this->errorMsg($res["msg"]);
            }
            if(0 < $order["pay_price"] && 1 < $order["pay_type"]) {
                $refund_model = new \app\massage\model\RefundOrder();
                $order["coach_refund_time"] = $update["coach_refund_time"];
                $order["coach_refund_text"] = $update["coach_refund_text"];
                $refund_id = $refund_model->coachRefundOrder($order, 1);
                $res = $refund_model->refundCashV2($this->payConfig($this->_uniacid, $order["app_pay"]), $order, $order["pay_price"], $refund_id);
                if(!empty($res["code"])) {
                    decCache($key, 1, $this->_uniacid);
                    \think\facade\Db::rollback();
                    $this->errorMsg($res["msg"]);
                }
                if(!in_array($res["status"], [2, 4])) {
                    decCache($key, 1, $this->_uniacid);
                    \think\facade\Db::rollback();
                    $this->errorMsg("退款失败，请重试2");
                }
            }
        }
        $this->model->dataUpdate(["id" => $input["order_id"]], $update);
        if($input["type"] == 5) {
            $coach_model = new \app\massage\model\Coach();
            $coach_model->coachCarPriceAccount($order, $this->payConfig($this->_uniacid, $order["app_pay"]));
        }
        $log_model = new \app\massage\model\OrderLog();
        $log_model->addLog($input["order_id"], $this->_uniacid, $input["type"], $order["pay_type"], 4, $this->getUserId());
        \think\facade\Db::commit();
        decCache($key, 1, $this->_uniacid);
        return $this->success(true);
    }
    public function orderChangeCoach()
    {
        $input = $this->_input;
        $refund_model = new \app\massage\model\RefundOrder();
        $refund_order = $refund_model->where(["order_id" => $input["order_id"]])->where("status", "in", [1, 4, 5])->count();
        if(!empty($refund_order)) {
            $this->errorMsg("该订单正在申请退款，请先处理再进行下一步");
        }
        $success_add_order = $this->model->dataInfo(["add_pid" => $input["order_id"], "pay_type" => 7]);
        if(!empty($success_add_order)) {
            $this->errorMsg("该订单加钟订单已经完成，无法转单");
        }
        $order = $this->model->dataInfo(["id" => $input["order_id"]]);
        $change_model = new \app\massage\model\CoachChangeLog();
        $coach_name = !empty($input["coach_name"]) ? $input["coach_name"] : "";
        $text = !empty($input["text"]) ? $input["text"] : "";
        $phone = !empty($input["mobile"]) ? $input["mobile"] : "";
        $admin_id = !empty($input["admin_id"]) ? $input["admin_id"] : 0;
        $res = $change_model->orderChangeCoach($order, $input["coach_id"], $this->_user["id"], $admin_id, $coach_name, $text, $phone, 2);
        if(!empty($res["code"])) {
            $this->errorMsg($res["msg"]);
        }
        return $this->success($res);
    }
    public function orderChangeCoachList()
    {
        $input = $this->_param;
        $coach_model = new \app\massage\model\Coach();
        $order = $this->model->dataInfo(["id" => $input["order_id"]]);
        $coach_id = $coach_model->getOrderServiceCoach($order);
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 2];
        $dis[] = ["id", "in", $coach_id];
        $dis[] = ["id", "<>", $order["coach_id"]];
        $dis[] = ["is_work", "=", 1];
        $dis[] = ["user_id", ">", 0];
        if(!empty($this->admin_info["admin_id"])) {
            $dis[] = ["admin_id", "in", $this->admin_info["admin_arr"]];
        }
        if(!empty($input["coach_name"])) {
            $dis[] = ["coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        if(!empty($order["store_id"])) {
            $dis[] = ["store_id", "=", $order["store_id"]];
        }
        $list = $coach_model->where($dis)->select()->toArray();
        $log_model = new \app\massage\model\CoachChangeLog();
        $arr = $log_model->getNearTimeCoach($order, $list);
        $top = !empty($input["type"]) && $input["type"] == 1 ? "distance asc,id desc" : "near_time asc,id desc";
        $lat = $order["address_info"]["lat"];
        $lng = $order["address_info"]["lng"];
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $data = $coach_model->where("id", "in", $arr)->field(["*", $alh])->order($top)->paginate(10)->toArray();
        if(!empty($data["data"])) {
            $admin_model = new \app\massage\model\Admin();
            $user_model = new \app\massage\model\User();
            foreach ($data["data"] as &$v) {
                $v["partner_name"] = $user_model->where(["id" => $v["partner_id"]])->value("nickName");
                $v["near_time"] = date("m-d H:i", $v["near_time"]);
                $v["admin_info"] = $admin_model->where(["id" => $v["admin_id"]])->field("city_type,agent_name as username")->find();
                $v["price"] = $coach_model->getCoachServicePrice($order, $v["id"]);
            }
        }
        return $this->success($data);
    }
    public function noticeUpdate()
    {
        $input = $this->_input;
        $notice_model = new \app\massage\model\NoticeList();
        $data = $notice_model->dataUpdate(["id" => $input["id"]], $input);
        return $this->success($data);
    }
    public function getVirtualPhone()
    {
        $input = $this->_input;
        $order_model = new \app\massage\model\Order();
        $order = $order_model->dataInfo(["id" => $input["order_id"]]);
        $called = new \app\virtual\model\Config();
        $phone = $this->admin_info["mobile"];
        if(empty($phone)) {
            $res = $order["address_info"]["mobile"];
        } else {
            $res = $called->getVirtual($order, 1, $phone);
        }
        return $this->success($res);
    }
    public function adminSelect()
    {
        $input = $this->_param;
        $dis = ["is_admin" => 0, "status" => 1, "uniacid" => $this->_uniacid];
        if(!empty($input["agent_coach_auth"])) {
            $dis["agent_coach_auth"] = $input["agent_coach_auth"];
        }
        $where = [];
        if(!empty($this->admin_info["admin_id"])) {
            $where[] = ["id", "in", $this->admin_info["admin_arr"]];
        }
        $admin_model = new \app\massage\model\Admin();
        $data = $admin_model->where($dis)->where($where)->field("id,username,agent_name")->select()->toArray();
        return $this->success($data);
    }
    public function passRefundV2()
    {
        $input = $this->_input;
        $order = $this->refund_order_model->dataInfo(["id" => $input["order_id"]]);
        $pay_order = $this->model->where(["id" => $order["order_id"]])->field("app_pay,pay_type,is_add")->find()->toArray();
        $is_app = $pay_order["app_pay"];
        if(empty($input["list"])) {
            $this->errorMsg("请选择商品");
        }
        $refund_goods_model = new \app\massage\model\RefundOrderGoods();
        $goods_price = $material_price = 0;
        foreach ($input["list"] as $value) {
            $num = $refund_goods_model->where(["id" => $value["id"]])->sum("num");
            $goods_price += $value["goods_price"] * $num;
            $material_price += $value["material_price"] * $num;
        }
        $input["refund_empty_cash"] = isset($input["refund_empty_cash"]) ? $input["refund_empty_cash"] : 0;
        $input["apply_empty_cash"] = isset($input["apply_empty_cash"]) ? $input["apply_empty_cash"] : 0;
        $input = $this->refund_order_model->emptyCashSet($input, $goods_price, $material_price, $pay_order["pay_type"], $pay_order["is_add"]);
        if(!empty($input["code"]) && $input["code"] == 500) {
            $this->errorMsg($input["msg"]);
        }
        $car_price = !empty($input["car_price"]) ? $input["car_price"] : 0;
        if(round($input["price"], 2) != round($goods_price + $material_price + $car_price - $input["refund_empty_cash"] - $input["refund_comm_cash"], 2)) {
            $this->errorMsg("请检查金额");
        }
        $goods_price = $goods_price - $input["comm_service_cash"] - $input["empty_service_cash"];
        $material_price = $material_price - $input["comm_material_cash"] - $input["empty_material_cash"];
        $this->refund_order_model->dataUpdate(["id" => $input["order_id"]], ["version" => 2]);
        foreach ($input["list"] as $value) {
            $update = ["goods_price" => $value["goods_price"], "material_price" => $value["material_price"], "comm_service_cash" => $value["comm_service_cash"], "comm_material_cash" => $value["comm_material_cash"], "refund_comm_cash" => $value["refund_comm_cash"], "empty_service_cash" => $value["empty_service_cash"], "empty_material_cash" => $value["empty_material_cash"], "refund_empty_cash" => $value["refund_empty_cash"], "apply_empty_service_cash" => $value["apply_empty_service_cash"], "apply_empty_material_cash" => $value["apply_empty_material_cash"], "apply_empty_cash" => $value["apply_empty_cash"]];
            $refund_goods_model->dataUpdate(["id" => $value["id"]], $update);
        }
        $res = $this->refund_order_model->passOrder($input["order_id"], $input["price"], $this->payConfig($this->_uniacid, $is_app), $this->admin_info["user_id"], $input["text"], 1, 3);
        if(!empty($res["code"])) {
            $this->errorMsg($res["msg"]);
        }
        $this->refund_order_model->dataUpdate(["id" => $input["order_id"]], ["refund_car_price" => $input["car_price"], "refund_service_price" => $goods_price, "refund_material_price" => $material_price]);
        return $this->success($res);
    }
    public function canRefundOrderInfo()
    {
        $input = $this->_param;
        $order_goods_model = new \app\massage\model\OrderGoods();
        $refund_model = new \app\massage\model\RefundOrder();
        $map[] = ["b.add_pid", "=", $input["order_id"]];
        $map[] = ["b.id", "=", $input["order_id"]];
        $price_log_model = new \app\massage\model\OrderPrice();
        $order = $price_log_model->alias("a")->join("massage_service_order_list b", "a.top_order_id = b.id")->where("b.pay_type", "not in", [7, -1, 1])->where(function ($query) {
            $query->whereOr($map);
        })->field("b.id as order_id,b.start_service_time,b.pay_type,b.car_price,b.discount,b.is_add,b.pay_model,b.true_car_price,b.start_time,b.end_time,b.true_service_price,b.material_price")->group("b.id")->order("b.is_add,b.id desc")->select()->toArray();
        if(empty($order)) {
            $this->errorMsg("订单状态错误1");
        }
        $arr["car_price"] = 0;
        $arr["discount"] = $arr["car_price"];
        foreach ($order as &$value) {
            $order_goods = $order_goods_model->where(["order_id" => $value["order_id"], "status" => 1])->where("can_refund_num", ">", 0)->field("id,goods_name,goods_cover,true_price,material_price,num,can_refund_num")->select()->toArray();
            $order_goods_list = [];
            if(!empty($order_goods)) {
                foreach ($order_goods as $v) {
                    $v["residue_service_price"] = $v["true_price"] * $v["can_refund_num"];
                    $v["residue_material_price"] = $v["material_price"] * $v["can_refund_num"];
                    $v["residue_service_price"] = $v["residue_service_price"] < $value["true_service_price"] ? $v["residue_service_price"] : $value["true_service_price"];
                    $v["residue_material_price"] = $v["residue_material_price"] < $value["material_price"] ? $v["residue_material_price"] : $value["material_price"];
                    $v["true_price"] = round($v["true_price"], 2);
                    $order_goods_list[] = $v;
                }
            }
            $value["order_goods"] = $order_goods_list;
            $arr["discount"] += $value["discount"];
            if($value["is_add"] == 0) {
                $arr["car_price"] = $refund_model->canRefundOrderPrice($value["order_id"]);
            }
        }
        $arr["order"] = $order;
        $refund_model = new \app\massage\model\RefundOrder();
        $have_empty = $refund_model->where(["order_id" => $input["order_id"]])->where("status", "in", [1, 2, 4, 5])->where("refund_empty_cash", ">", 0)->count();
        if(!empty($have_empty)) {
            $arr["empty_order_cash"] = 0;
        } else {
            $arr["empty_order_cash"] = getConfigSetting($this->_uniacid, "empty_order_cash");
        }
        $arr["after_service_can_refund"] = getConfigSetting($this->_uniacid, "after_service_can_refund");
        $fee_model = new \app\massage\model\EmptyTicketFeeConfig();
        $arr["cash_list"] = $fee_model->where(["uniacid" => $this->_uniacid])->select()->toArray();
        return $this->success($arr);
    }
    public function applyOrderRefund()
    {
        $input = $this->_input;
        if(empty($input["order"])) {
            $this->errorMsg("请勾选订单");
        }
        $refund_model = new \app\massage\model\RefundOrder();
        $input["order"] = arraySort($input["order"], "order_id", "desc");
        foreach ($input["order"] as $value) {
            $key = "order_refund_key_cant_refund" . $value["order_id"];
            incCache($key, 1, $this->_uniacid, 20);
            if(getCache($key, $this->_uniacid) == 1) {
                \think\facade\Db::startTrans();
                $order = $this->model->dataInfo(["id" => $value["order_id"]]);
                $car_price = !empty($value["car_price"]) ? $value["car_price"] : 0;
                if(empty($order)) {
                    \think\facade\Db::rollback();
                    decCache($key, 1, $this->_uniacid);
                    $this->errorMsg("订单未找到");
                }
                $add_order = $this->model->where(["add_pid" => $order["id"]])->where("pay_type", ">", 1)->count();
                if(0 < $add_order) {
                    \think\facade\Db::rollback();
                    decCache($key, 1, $this->_uniacid);
                    $this->errorMsg("请先申请加钟订单退款");
                }
                $can_refund_num = array_sum(array_column($order["order_goods"], "can_refund_num"));
                if($can_refund_num <= 0) {
                    $this->errorMsg("数量已退完");
                }
                if(empty($value["list"]) && $car_price <= 0) {
                    \think\facade\Db::rollback();
                    decCache($key, 1, $this->_uniacid);
                    $this->errorMsg("请选择商品");
                }
                if($order["pay_type"] == 7) {
                    \think\facade\Db::rollback();
                    decCache($key, 1, $this->_uniacid);
                    $this->errorMsg("核销后不能退款");
                }
                $refund_empty_cash = isset($value["refund_empty_cash"]) ? $value["refund_empty_cash"] : 0;
                $apply_empty_cash = isset($value["apply_empty_cash"]) ? $value["apply_empty_cash"] : 0;
                $comm_balance = isset($value["comm_balance"]) ? $value["comm_balance"] : 0;
                $res = $refund_model->applyRefundAdmin($order, $value["list"], $car_price, $this->admin_info["user_id"], 3, $refund_empty_cash, $apply_empty_cash, $comm_balance);
                if(!empty($res["code"])) {
                    \think\facade\Db::rollback();
                    decCache($key, 1, $this->_uniacid);
                    $this->errorMsg($res["msg"]);
                }
                $refund_order = $this->refund_order_model->dataInfo(["id" => $res]);
                $res = $this->refund_order_model->passOrder($res, $refund_order["apply_price"], $this->payConfig($this->_uniacid, $order["app_pay"]), $this->admin_info["user_id"], "", 1, 3);
                if(!empty($res["code"])) {
                    decCache($key, 1, $this->_uniacid);
                    $this->errorMsg($res["msg"]);
                }
                decCache($key, 1, $this->_uniacid);
                \think\facade\Db::commit();
            } else {
                decCache($key, 1, $this->_uniacid);
                $this->errorMsg("该订单正在发起退款，请稍后再试");
            }
        }
        return $this->success(true);
    }
    public function abnOrderInfo()
    {
        $input = $this->_param;
        $order_model = new \app\abnormalorder\model\OrderList();
        $handle_model = new \app\abnormalorder\model\OrderInfoHandle();
        $data = $order_model->dataInfo(["id" => $input["id"]]);
        $data = $order_model->getOrderResult($data, 1);
        $data["deduct_time"] = $handle_model->where(["order_id" => $data["id"]])->where("status", ">", 1)->where("deduct_cash", ">", 0)->value("create_time");
        $data["deduct_time"] = !empty($data["deduct_time"]) ? date("Y-m-d H:i:s", $data["deduct_time"]) : "";
        $arr["info"] = $data;
        return $this->success($arr);
    }
}

?>